package com.muqi.app.im.adapter;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
	private FragmentManager fm;
	private List<Fragment> fragmentList;
	private boolean destoryPager;

	public MyFragmentPagerAdapter(FragmentManager fm) {
		super(fm);
	}

	public MyFragmentPagerAdapter(FragmentManager fm,List<Fragment> fragmentList) {
		super(fm);
		this.fm = fm;
		this.fragmentList = fragmentList;
	}
	
	public MyFragmentPagerAdapter(FragmentManager fragmentManager, List<Fragment> fragmentList, boolean destoryPager) {
		super(fragmentManager);
		this.fragmentList = fragmentList;
		this.destoryPager = destoryPager;
	}

	@Override
	public Fragment getItem(int position) {
		Fragment fragment = null;
		fragment = fragmentList.get(position);
		Bundle bundle = new Bundle();
		bundle.putString("id", "" + position);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public int getCount() {
		return fragmentList.size();
	}

	@Override
	public Fragment instantiateItem(ViewGroup container, int position) {
		Fragment fragment = (Fragment) super.instantiateItem(container,position);
		fm.beginTransaction().show(fragment).commit();
		return fragment;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		if(destoryPager){
			super.destroyItem(container, position, object);
		}else{
			Fragment fragment = fragmentList.get(position);
			fm.beginTransaction().hide(fragment).commit();
		}
	}
}
